<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户编号" prop="customerNumber">
        <el-input v-model="queryParams.customerNumber" placeholder="请输入客户编号" clearable
          @keyup.enter.native="handleQueryByCode" ref="ipt" />
      </el-form-item>
      <el-form-item label="客户级别" prop="vip">
        <el-input v-model="queryParams.vip" placeholder="请输入客户级别" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="地区" prop="area">
        <el-input v-model="queryParams.area" placeholder="请输入地区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="公司" prop="company">
        <el-input v-model="queryParams.company" placeholder="请输入公司" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="座位" prop="position">
        <el-input v-model="queryParams.position" placeholder="请输入座位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="职位" prop="post">
        <el-input v-model="queryParams.post" placeholder="请输入职位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客源" prop="customerSource">
        <el-input v-model="queryParams.customerSource" placeholder="请输入客源" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="证件资料" prop="documentInformation">
        <el-input v-model="queryParams.documentInformation" placeholder="请输入证件资料" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="queryParams.age" placeholder="请输入年龄" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="图像" prop="avatar">
        <el-input v-model="queryParams.avatar" placeholder="请输入图像" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="称谓" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入称谓" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否停用" prop="isEnable">
        <el-input v-model="queryParams.isEnable" placeholder="请输入是否停用" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryByCode">保存</el-button> -->
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="danger" icon="el-icon-full-screen" size="mini" @click="focus">开始扫码</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['bussiness:userMeeting:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['bussiness:userMeeting:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bussiness:userMeeting:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['bussiness:userMeeting:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userMeetingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="客户编号" align="center" prop="customerNumber" />
      <el-table-column label="客户级别" align="center" prop="vip" />
      <el-table-column label="主类别" align="center" prop="mainType" />
      <el-table-column label="详细类别" align="center" prop="detailType" />
      <el-table-column label="地区" align="center" prop="area" />
      <el-table-column label="公司" align="center" prop="company" />
      <el-table-column label="客户名称" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="座位" align="center" prop="position" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="职位" align="center" prop="post" />
      <el-table-column label="客源" align="center" prop="customerSource" />
      <el-table-column label="证件资料" align="center" prop="documentInformation" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="图像" align="center" prop="avatar" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="称谓" align="center" prop="nickName" />
      <el-table-column label="是否停用" align="center" prop="isEnable" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handlePositon(scope.row)"
            v-hasPermi="['bussiness:userMeeting:edit']">修改座位</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bussiness:userMeeting:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bussiness:userMeeting:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参会人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户编号" prop="customerNumber">
          <el-input v-model="form.customerNumber" placeholder="请输入客户编号" :disabled="isPos" />
        </el-form-item>
        <el-form-item label="客户级别" prop="vip" v-if="!isPos">
          <el-input v-model="form.vip" placeholder="请输入客户级别" />
        </el-form-item>
        <el-form-item label="地区" prop="area" v-if="!isPos">
          <el-input v-model="form.area" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="公司" prop="company" v-if="!isPos" :disabled="isPos">
          <el-input v-model="form.company" placeholder="请输入公司" />
        </el-form-item>
        <el-form-item label="客户名称" prop="name" :disabled="isPos">
          <el-input v-model="form.name" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="电话" prop="phone" v-if="!isPos">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="座位" prop="position">
          <el-input v-model="form.position" placeholder="请输入座位" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email" v-if="!isPos">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="职位" prop="post" v-if="!isPos">
          <el-input v-model="form.post" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="客源" prop="customerSource" v-if="!isPos">
          <el-input v-model="form.customerSource" placeholder="请输入客源" />
        </el-form-item>
        <el-form-item label="证件资料" prop="documentInformation" v-if="!isPos">
          <el-input v-model="form.documentInformation" placeholder="请输入证件资料" />
        </el-form-item>
        <el-form-item label="年龄" prop="age" v-if="!isPos">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="gender" v-if="!isPos">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图像" prop="avatar" v-if="!isPos">
          <el-input v-model="form.avatar" placeholder="请输入图像" />
        </el-form-item>
        <el-form-item label="称谓" prop="nickName" v-if="!isPos">
          <el-input v-model="form.nickName" placeholder="请输入称谓" />
        </el-form-item>
        <el-form-item label="是否停用" prop="isEnable" v-if="!isPos">
          <el-input v-model="form.isEnable" placeholder="请输入是否停用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserMeeting, getUserMeeting, delUserMeeting, addUserMeeting, updateUserMeeting, sign } from "@/api/bussiness/userMeeting";

export default {
  name: "UserMeeting",
  dicts: ['sys_user_sex'],
  data() {
    return {
      isPos: false,
      isSign: false,//是否扫码中
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参会人员信息表格数据
      userMeetingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerNumber: null,
        vip: null,
        mainType: null,
        detailType: null,
        area: null,
        company: null,
        name: null,
        phone: null,
        position: null,
        email: null,
        post: null,
        customerSource: null,
        documentInformation: null,
        age: null,
        gender: null,
        avatar: null,
        type: null,
        nickName: null,
        isEnable: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      channel: null
    };
  },
  created() {
    this.channel = new BroadcastChannel('myChannel');
    this.getList();
  },
  mounted() {

    this.focus()

    // window.addEventListener('click', this.focus)
  },
  beforeDestroy() {

    // window.removeEventListener('click', this.focus)
  },
  methods: {
    focus() {

      this.$refs.ipt && this.$refs.ipt.focus()
    },
    sendMsg(msg) {
      this.channel.postMessage(msg)
    },
    async sign(customerNumber) {
      this.isSign = true
      const res = await sign({ activeId: 1, customerNumber })
      this.isSign = false

      if (res.code == 200) {
        this.queryParams.customerNumber = null
        this.sendMsg(customerNumber)
      }
    },
    handleQueryByCode() {
      this.queryParams.pageNum = 1;
      this.getList()
      this.sign(this.queryParams.customerNumber)

    },
    /** 查询参会人员信息列表 */
    getList() {
      this.loading = true;
      listUserMeeting(this.queryParams).then(response => {
        this.userMeetingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerNumber: null,
        vip: null,
        mainType: null,
        detailType: null,
        area: null,
        company: null,
        name: null,
        phone: null,
        position: null,
        email: null,
        post: null,
        customerSource: null,
        documentInformation: null,
        age: null,
        gender: null,
        avatar: null,
        type: null,
        createTime: null,
        updateTime: null,
        nickName: null,
        isEnable: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {

      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isPos = false
      this.open = true;
      this.title = "添加参会人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.isPos = false
      getUserMeeting(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参会人员信息";
      });
    },

    // 修改昨晚
    handlePositon(row) {
      this.reset();
      this.isPos = true
      const id = row.id || this.ids
      getUserMeeting(id).then(response => {
        this.form = response.data;
        this.open = true;

        this.title = "修改座位";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserMeeting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserMeeting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除参会人员信息编号为"' + ids + '"的数据项？').then(function () {
        return delUserMeeting(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bussiness/userMeeting/export', {
        ...this.queryParams
      }, `userMeeting_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
